﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RedHouse.Data.Service;
using RedHouse.Data.Common;
using RedHouse.Data.Entities;

namespace RedHouse.Data.Base
{
    public class SubcribeService : ISubcribeService
    {
        protected NewRedHouseEntitiesDataContext _dataContext;
        private static SubcribeService _instance;
        public static SubcribeService Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new SubcribeService();
                }
                return _instance;
            }
        }
        public SubcribeService()
        {
            _dataContext = new NewRedHouseEntitiesDataContext();
        }

        public Common.OperationResult SaveSubcribe(Subcribe subcribe)
        {
            OperationResult result = new OperationResult()
            {
                Message = "Thanks for your subcription!",
                Result = true,
                ReturnObject = subcribe
            };

            try
            {
                _dataContext.Subcribes.InsertOnSubmit(subcribe);
                _dataContext.SubmitChanges();
                SendConfirmationEmail(subcribe.Email);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Result = false;
            }
            return result;
        }

        private void SendConfirmationEmail(string toEmail)
        {
            string confirmTemplate = string.Format(@"<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'
                'http://www.w3.org/TR/html4/loose.dtd'>
                <html lang='en'>
                <head>
                </head>
                <body>
                    <div style='width: auto; margin: auto;font-family: Times New Roman;font-size: 18px;'>
                        <div style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;padding-top: 10px; height: 85px; background: #535353;'>
                            <p style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;color: white; text-align: center;'>Nếu không xem được nội dung email, xin vui lòng xem <a href='http://redhouse.com.vn/newsletter/20111208/index.htm' style='color: #d02419; cursor: pointer;'>tại đây</a></p>
                            <p style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;color: white; text-align: center;'>If you cannot see the content of this email, please <a href='http://redhouse.com.vn/newsletter/20111208/index.htm' style='color: #d02419; cursor: pointer;'>click here</a></p>
                            <p style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;color: white; text-align: center;'>Nhấn vào đây để xác nhận email <a href='http://redhouse.com.vn/MailConfirm.aspx?email={0}' style='color: #d02419; cursor: pointer;'>tại đây</a></p>
                            <p style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;color: white; text-align: center;'>Click here to confirm your subcription <a href='http://redhouse.com.vn/MailConfirm.aspx?email={0}' style='color: #d02419; cursor: pointer;'>click here</a></p>
                        </div>
        
                        <div style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;width: 800px; margin: auto;'>
                            <div style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;'>
                                <img style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;float:left' src='http://redhouse.com.vn/newsletter/20111208/images/logo-1.png' />
                                <h1 style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;color: #d02419;float: left;font-size: 35px;font-style: oblique;padding-top: 66px;padding-left:99px;'>Welcome to RedHouse !!!</h1>
                                <div style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;clear:both;'></div>
                            </div>
            
                            <div style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;'>
                                <img style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;' src='http://redhouse.com.vn/newsletter/20111208/images/img-1.png' />
                            </div>
            
                            <div style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;background: url(http://redhouse.com.vn/newsletter/20111208/images/bg-1.png) repeat;padding: 30px 60px 30px 60px;width: 676px;'>             
                                <p style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;color: #d02419;line-height: 1.5;text-align: justify;'>Cảm ơn các bạn đã đăng ký email với nhà hàng RedHouse (Nhà Đỏ)</p>
                                <p style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;color: #d02419;line-height: 1.5;text-align: justify;'>Đăng ký nhận email từ RedHouse bạn sẽ có cơ hội:</p>
                                <p style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;color: #d02419;line-height: 1.5;text-align: justify;'>- Là người đầu tiên biết được thông tin về chương trình khuyến mãi</p>
                                <p style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;color: #d02419;line-height: 1.5;text-align: justify;'>- Tham gia các chương trình khuyến mãi dành riêng cho khách hàng thân thiết</p>
                                <p style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;color: #d02419;line-height: 1.5;text-align: justify;'>- Nhận được thông tin về ẩm thực, ăn uống, sức khỏe, v.v ...</p>
                                <p style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;color: #d02419;line-height: 1.5;text-align: justify;font-style:oblique;'>Bạn hãy lưu địa chỉ email của RedHouse vào contact để đảm bảo ko bị lỡ mất thông tin nào từ RedHouse nhé!
                </p>
                                <br /><br />

                                <p style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;color: #d02419;line-height: 1.5;text-align: justify;'>Thank you for subscribing to RedHouse Restaurant.</p>
                                <p style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;color: #d02419;line-height: 1.5;text-align: justify;'>Subscribing to RedHouse brings you the opportunities to:</p>
                                <p style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;color: #d02419;line-height: 1.5;text-align: justify;'>- Be the first to know about our promotions</p>
                                <p style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;color: #d02419;line-height: 1.5;text-align: justify;'>- Be eligible for programs exclusive for loyal customers</p>
                                <p style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;color: #d02419;line-height: 1.5;text-align: justify;'>- Receive useful information about cuisine, dining, health, etc</p>
                                <p style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;color: #d02419;line-height: 1.5;text-align: justify;font-style:oblique;'>Please add RedHouse email to your contact to make sure you 
                are not going to miss any interesting news from RedHouse 
                in the future!</p>
                            </div>
                        </div>
        
                        <div style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;height: 187px;background: url(http://redhouse.com.vn/newsletter/20111208/images/bg-2.png) no-repeat;width: 800px;margin: auto;'>
                            <!-- Hà N?i -->
                            <div style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;width: 300px;float: left;padding: 60px 0px 0px 60px;'>
                                <p style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;color: #d02419;line-height: 1.5;text-align: justify;'>6A Hàm Long, Q. Hoàn Kiếm, Hà Nội</p>
                                <p style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;color: #d02419;line-height: 1.5;text-align: justify;'>Tel: (844) 3944 9668</p>
                                <p style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;color: #d02419;line-height: 1.5;text-align: justify;'>Hotline: (84) 012 3933 3399</p>
                                <p style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;color: #d02419;line-height: 1.5;text-align: justify;'>Email: infohanoi@redhouse.com.vn</p>
                            </div>
            
                            <!-- TP. HCM -->
                            <div style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;width: 350px;float: left;padding: 60px 0px 0px 60px;'>
                                <p style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;color: #d02419;line-height: 1.5;text-align: justify;'>79 Ngô Thời Nhiệm, Q.3, Tp. Hồ Chí Minh</p>
                                <p style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;color: #d02419;line-height: 1.5;text-align: justify;'>Tel: (848) 3933 3399</p>
                                <p style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;color: #d02419;line-height: 1.5;text-align: justify;'>Hotline: (84) 093 647 1888</p>
                                <p style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;color: #d02419;line-height: 1.5;text-align: justify;'>Email: info@redhouse.com.vn</p>
                            </div>
                            <div style='margin:0px;padding: 0px;font-family: Times New Roman;font-size: 18px;clear:both;'></div>
                        </div>
                    </div>
                </body>
                </html>'", toEmail);

            string plainBody = string.Format(@"Nếu không xem được nội dung email, xin vui lòng xem tại link sau: http://redhouse.com.vn/newsletter/20111208/index.htm
                                 If you cannot see the content of this email, please click this link: http://redhouse.com.vn/newsletter/20111208/index.htm
                                 Nhấn vào đây để xác nhận email http://redhouse.com.vn/contact.aspx?email={0}
                                 Click here to confirm your subcription http://redhouse.com.vn/contact.aspx?email={0}",toEmail);

            RedHouseEmail email = new RedHouseEmail()
            {
                ToList = new List<string>() { toEmail },
                From = "no-reply@redhouse.com.vn",
                Password = "123456789",
                Subject = "[RedHouse] Subcribe confirmation",
                CcList = new List<string>(),
                BccList = new List<string>(),
                Body = confirmTemplate,
                PlainBody = plainBody
            };

            EmailService.Instance.SendEmail(email);
        }

        public bool CheckExistingEmail(string email)
        {
            Subcribe subcribe = _dataContext.Subcribes.Where(s => s.Email.ToLower().Trim().Equals(email.ToLower().Trim())).FirstOrDefault();
            return subcribe != null;
        }

        public Common.OperationResult UpdateSubcribe(Subcribe subcribe)
        {
            OperationResult result = new OperationResult()
            {
                Message = string.Empty,
                Result = true,
                ReturnObject = subcribe
            };

            try
            {
                _dataContext.Subcribes.Attach(subcribe, true);
                _dataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Result = false;
            }
            return result;
        }

        private void NotifyEmailExisted()
        {
        }

        public Common.OperationResult ActiveSubcribe(Subcribe subcribe)
        {
            OperationResult result = new OperationResult()
            {
                Message = string.Empty,
                Result = true,
                ReturnObject = subcribe
            };

            try
            {
                Subcribe updateSubcribe = _dataContext.Subcribes.Where(s => s.Email.Trim().ToLower().Equals(subcribe.Email.ToLower())).FirstOrDefault();
                updateSubcribe.State = 1;
                _dataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Result = false;
            }
            return result;
        }

        public IList<Subcribe> GetAllSubcribe()
        {
            return _dataContext.Subcribes.ToList();
        }

        public Common.OperationResult DeleteSubcribe(Guid id)
        {
            OperationResult result = new OperationResult()
            {
                Message = string.Empty,
                Result = true
            };

            try
            {
                Subcribe subcribe = _dataContext.Subcribes.Where(s => s.ID == id).FirstOrDefault();
                _dataContext.Subcribes.DeleteOnSubmit(subcribe);
                _dataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Result = false;
            }
            return result;
        }
    }
}
